package com.wnhz.work.yz.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.wnhz.work.yz.MainActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.LibraryActivity;
import com.wnhz.work.yz.activity.VideoActivity;
import com.wnhz.work.yz.adapter.BrandAdapter;
import com.wnhz.work.yz.bean.BrandBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.ListViewForScrollView;
import com.wnhz.work.yz.view.MyScrollView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * ---品牌
 */
public class Home1Brand extends Fragment implements View.OnClickListener {
    private View view;
    private MyScrollView myScroll;
    private ListViewForScrollView my_listView;
    private RelativeLayout rl_type;
    private ImageView iv_head;
    private List<BrandBean.InfoEntity> info   = new ArrayList<>();
    private BrandAdapter mAdapter;

    private BrandBean bean;
    private MainActivity mainActivity;


    private String type = "1";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home1_brand, container, false);
        mainActivity = (MainActivity) getActivity();

        initData();
        return view;
    }

    private void upLoad() {
        XUtil.Post(Url.WENKU_GETVIDEOLIST, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e("222---", result);

                Gson g = new Gson();
                BrandBean bean=g.fromJson(result,BrandBean.class);
                info=bean.getInfo();
            }

            @Override
            public void onFinished() {
                initView();
                super.onFinished();
            }
        });
    }

    private void initData() {
        upLoad();
    }

    private void initView() {
        myScroll = (MyScrollView) view.findViewById(R.id.myScroll);
        my_listView = (ListViewForScrollView) view.findViewById(R.id.my_listView);
        rl_type = (RelativeLayout) view.findViewById(R.id.rl_type);
        iv_head = (ImageView) view.findViewById(R.id.iv_head);

        view.findViewById(R.id.rl_head).setOnClickListener(this);//顶部
        view.findViewById(R.id.rl_library).setOnClickListener(this);//文库
        view.findViewById(R.id.rl_record).setOnClickListener(this);//档案
        view.findViewById(R.id.rl_video).setOnClickListener(this);//视频

        iv_head.setImageResource(R.mipmap.img_pingbai1);
        if ("1".equals(type)) {
            rl_type.setVisibility(View.GONE);
        } else {
            rl_type.setVisibility(View.VISIBLE);
        }

        mAdapter = new BrandAdapter(getActivity(), info);
        my_listView.setAdapter(mAdapter);
        myScroll.smoothScrollTo(0, 0);
    }

    @Override
    public void onResume() {
        if (myScroll != null)
            myScroll.smoothScrollTo(0, 0);
        super.onResume();
    }

    @Override
    public void onStart() {
        if (myScroll != null)
            myScroll.smoothScrollTo(0, 0);
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            //顶部跳转
            case R.id.rl_head:
                //  mainActivity.MyToast("跳转");
                break;

            //文库
            case R.id.rl_library:
                startActivity(new Intent(getContext(), LibraryActivity.class));
                break;

            //档案
            case R.id.rl_record:
                //   mainActivity.MyToast("档案");
                break;

            //视频
            case R.id.rl_video:
                intent = new Intent(getActivity(), VideoActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}
